/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Scenario2Panel.java
 *
 * Created on 12-ene-2009, 10:38:41
 */
package mrs.gui;

import java.io.File;
import java.util.Vector;
import javax.swing.*;

import mrs.models.*;
import mrs.logging.*;
import mrs.ontology.*;

import javax.swing.JFileChooser;
import mrs.util.RecipeParser;

/**
 *
 * @author Pablo
 */
public class Scenario2Panel extends javax.swing.JPanel {

    /** Creates new form Scenario2Panel */
    public Scenario2Panel() {
        initComponents();
    }

    public void initGUI() throws Exception {
        Logger.log.debug("GUI init START");
        this.reloadRecipeFiles();
        this.clearRecipePanel();
        Logger.log.debug("GUI init END");
    }

    public void showRecipe(Recipe recipe)
    {
        this.jTextFieldRecipeTitle.setText(recipe.title);
        this.jTextFieldRecipeServings.setText(recipe.servings);
        this.jTextAreaRecipeDirections.setText(recipe.directions);
    }
    private void reloadRecipeFiles() {
        this.jListRecipeFiles = new JList(Recipe.getRecipeFiles());
        this.jListRecipeFiles.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        this.jListRecipeFiles.setSelectedIndex(0);

        this.jScrollPaneRecipeFiles.setViewportView(jListRecipeFiles);
    }

    private void clearRecipePanel() {
        this.jTextFieldRecipeTitle.setText("");
        this.jTextFieldRecipeServings.setText("");
        this.jTextAreaRecipeDirections.setText("");
        this.jListRecipeIngredients = new JList();
        this.jListRecipeIngredients.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        this.jListRecipeIngredients.setSelectedIndex(0);
        this.jScrollPaneRecipeIngredients.setViewportView(jListRecipeIngredients);
        Recipe.getRecipeFiles();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneRecipeFiles = new javax.swing.JScrollPane();
        jListRecipeFiles = new javax.swing.JList();
        jLabelRecipeFiles = new javax.swing.JLabel();
        jButtonImportRecipe = new javax.swing.JButton();
        jPanelRecipe = new javax.swing.JPanel();
        jLabelRecipeTitle = new javax.swing.JLabel();
        jTextFieldRecipeTitle = new javax.swing.JTextField();
        jLabelRecipeServings = new javax.swing.JLabel();
        jTextFieldRecipeServings = new javax.swing.JTextField();
        jLabelIngredients = new javax.swing.JLabel();
        jScrollPaneRecipeIngredients = new javax.swing.JScrollPane();
        jListRecipeIngredients = new javax.swing.JList();
        jLabelDirections = new javax.swing.JLabel();
        jScrollPaneRecipeDirections = new javax.swing.JScrollPane();
        jTextAreaRecipeDirections = new javax.swing.JTextArea();

        jScrollPaneRecipeFiles.setViewportView(jListRecipeFiles);

        jLabelRecipeFiles.setText("RecipeML Files");

        jButtonImportRecipe.setText(" Import selected recipe >>");
        jButtonImportRecipe.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonImportRecipeMouseClicked(evt);
            }
        });

        jPanelRecipe.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabelRecipeTitle.setText("TITLE");

        jLabelRecipeServings.setText("SERVINGS");

        jLabelIngredients.setText("INGREDIENTS");

        jScrollPaneRecipeIngredients.setViewportView(jListRecipeIngredients);

        jLabelDirections.setText("DIRECTIONS");

        jTextAreaRecipeDirections.setColumns(20);
        jTextAreaRecipeDirections.setRows(5);
        jScrollPaneRecipeDirections.setViewportView(jTextAreaRecipeDirections);

        javax.swing.GroupLayout jPanelRecipeLayout = new javax.swing.GroupLayout(jPanelRecipe);
        jPanelRecipe.setLayout(jPanelRecipeLayout);
        jPanelRecipeLayout.setHorizontalGroup(
            jPanelRecipeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRecipeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelRecipeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPaneRecipeDirections, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(jScrollPaneRecipeIngredients, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelRecipeLayout.createSequentialGroup()
                        .addComponent(jLabelRecipeTitle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldRecipeTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelRecipeLayout.createSequentialGroup()
                        .addComponent(jLabelRecipeServings)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldRecipeServings, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelIngredients, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDirections, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanelRecipeLayout.setVerticalGroup(
            jPanelRecipeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRecipeLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelRecipeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelRecipeTitle)
                    .addComponent(jTextFieldRecipeTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelRecipeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelRecipeServings)
                    .addComponent(jTextFieldRecipeServings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelIngredients)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPaneRecipeIngredients, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabelDirections)
                .addGap(1, 1, 1)
                .addComponent(jScrollPaneRecipeDirections, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonImportRecipe)
                        .addContainerGap())
                    .addComponent(jLabelRecipeFiles)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPaneRecipeFiles, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanelRecipe, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelRecipeFiles)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPaneRecipeFiles, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonImportRecipe)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelRecipe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jButtonImportRecipe.getAccessibleContext().setAccessibleName("jButtonImportRecipe");
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonImportRecipeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonImportRecipeMouseClicked
        // Crear receta nueva parseando fichero seleccionado
        File selectedFile = (File) this.jListRecipeFiles.getSelectedValue();
        System.out.println(selectedFile.getAbsolutePath());

        Recipe importedRecipe = RecipeParser.parse(selectedFile.getAbsolutePath());
        this.showRecipe(importedRecipe);

        // Mostrarla en el GUI

    }//GEN-LAST:event_jButtonImportRecipeMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonImportRecipe;
    private javax.swing.JLabel jLabelDirections;
    private javax.swing.JLabel jLabelIngredients;
    private javax.swing.JLabel jLabelRecipeFiles;
    private javax.swing.JLabel jLabelRecipeServings;
    private javax.swing.JLabel jLabelRecipeTitle;
    private javax.swing.JList jListRecipeFiles;
    private javax.swing.JList jListRecipeIngredients;
    private javax.swing.JPanel jPanelRecipe;
    private javax.swing.JScrollPane jScrollPaneRecipeDirections;
    private javax.swing.JScrollPane jScrollPaneRecipeFiles;
    private javax.swing.JScrollPane jScrollPaneRecipeIngredients;
    private javax.swing.JTextArea jTextAreaRecipeDirections;
    private javax.swing.JTextField jTextFieldRecipeServings;
    private javax.swing.JTextField jTextFieldRecipeTitle;
    // End of variables declaration//GEN-END:variables
}
